import router from './router'
import store from './store'
// import { Message } from 'element-ui'
// 进度条
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/404'] // no redirect whitelist
// todo 路由守卫 实现页面访问权限控制
router.beforeEach(async(to, from, next) => {
  // 开启进度条
  NProgress.start()
  // 判断vuex中是否有token
  if (store.getters.token) {
    console.log('判断是否去登录页')
    if (to.path === '/login') {
      console.log('已经在登录 跳转到主页')
      // 如果next指定(地址)跳转页面  就不会执行NProgress.done(不会关闭进度条)需要手动关闭
      next('/')
      NProgress.done()
    } else {
      console.log('已经登录 放行')
      // 获取用户信息 存储到vuex中
      if (!store.getters.userId) {
        await store.dispatch('user/getInfo')
      }
      next()
    }
  } else {
    console.log('判断是否去白名单')
    if (whiteList.includes(to.path)) {
      console.log('去白名单放行')
      next()
    } else {
      console.log('不在白名单 登录页')
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // 关闭进度条
  NProgress.done()
  // finish progress bar
  // NProgress.done()
})
